import java.io.File;

import property.Property;

public class GBLocator {
	public static void main(String[] args) {
        /* You need to manually specify three program arguments in Eclipse or Intellij before running */
		try {
			if (args.length == 0) {
				showHelp();
			} else {
				boolean isLegal = parseArgs(args);
				if (isLegal) {
					Core core = new Core();
					core.process();
				}
			}
		} catch (Exception ex) {
			showHelp();
		}
	}

	private static void showHelp() {
		String usage = "Usage:java -jar GBLocator [-options] \r\n\r\nwhere options must include:\r\n"
				+ "-d	indicates the path of the dataset\r\n"
				+ "-p	indicates which project to evaluate (available option: swt(0), aspectj(1),eclipse(2))\r\n"
				+ "-c	indicates if use the commitLog).";

		System.out.println(usage);
	}

	private static boolean parseArgs(String[] args) {
		int i = 0;
		String[] projectName = {"swt","aspectj","eclipse"};
		String bugFilePath = "";
		String sourceCodeDir = "";
        String datasetPath = "";
		String projectStr = "";
		float alpha = 0.3f;
		boolean flag = true;
		while (i < args.length - 1) {
			if (args[i].equals("-d")) {
				i++;
				datasetPath = args[i];
			} else if (args[i].equals("-p")) {
				i++;
				int projectnum = Integer.parseInt(args[i]);
				projectStr = projectName[projectnum];
			} else if (args[i].equals("-c")) {
				i++;
				flag = Boolean.parseBoolean(args[i]);
			}
			i++;
		}
        if(projectStr.compareTo("eclipse")==0){
            bugFilePath = datasetPath+"EclipseBugRepository.xml";
            sourceCodeDir = datasetPath+"eclipse-3.1/";
        }
        else if(projectStr.compareTo("swt")==0){
            bugFilePath = datasetPath+"SWTBugRepository.xml";
            sourceCodeDir = datasetPath+"swt-3.1/";
        }
        else{
            bugFilePath = datasetPath+"AspectJBugRepository.xml";
            sourceCodeDir = datasetPath+"aspectj/";
        }
		boolean isLegal = true;
		if (datasetPath.equals("") || datasetPath == null) {
			isLegal = false;
			System.out.println("you must indicate the path of your dataset");
		}
		if (projectStr.equals("") || projectStr == null) {
			isLegal = false;
			System.out.println("you must indicate which project to evaluate");
		}
		if (!isLegal) {
			showHelp();
		} else {
			File file = new File(System.getProperty("user.dir"));
			if (file.getFreeSpace() / 1024 / 1024 / 1024 < 2) {
				System.out
						.println("Not enough free disk space, please ensure your current disk space are bigger than 2G.");
				isLegal = false;
			} else {				
				File dir = new File(projectStr+"Tmp");
				if (!dir.exists()) {
					dir.mkdir();
				}
				Property.createInstance(bugFilePath, sourceCodeDir, dir
						.getAbsolutePath(), alpha, flag,projectStr, new File(sourceCodeDir).getAbsolutePath().length()+1);
			}

		}

		return isLegal;
	}
}